package org.filecollector.filecontroller.virtualcatalogue;

import lombok.NonNull;
import org.filecollector.filecontroller.virtualcatalogue.virtualfile.VirtualFile;
import org.filecollector.filecontroller.virtualcatalogue.virtualfile.InvalidFileException;
import org.filecollector.filecontroller.virtualcatalogue.virtualfile.VirtualFileDependencies;

import java.io.File;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Record contains all package dependencies
 * @param warner logger interface
 * @param informer logger interface
 * @param debugger logger interface
 * @param error logger interface
 * @param troubleHuntPutter consumer that get exceptions
 * @param recursiveRemover util to recursive removing
 * @param standardDependenciesVFProducer function that produce dependencies for
 * {@link VirtualFile VirtualFile}
 */
public record VirtualCatalogueDependencies(@NonNull Consumer<String> warner, @NonNull Consumer<String> informer,
                                           @NonNull Consumer<String> debugger,
                                           @NonNull Consumer<String> error,
                                           @NonNull Consumer<InvalidFileException> troubleHuntPutter,
                                           @NonNull Function<File, Boolean> recursiveRemover,
                                           @NonNull Function<Long, VirtualFileDependencies> standardDependenciesVFProducer) {

    private static boolean testingRecursiveRemover(File directory) {
        File[] contents = directory.listFiles();
        if (contents != null) {
            for (File file : contents) {
                testingRecursiveRemover(file);
            }
        }
        return directory.delete();
    }

    public static VirtualCatalogueDependencies getTest() {
        return new VirtualCatalogueDependencies(
                (String _) -> {},
                (String _) -> {},
                (String _) -> {},
                (String _) -> {},
                (InvalidFileException _) -> {},
                VirtualCatalogueDependencies::testingRecursiveRemover,
                (Long _) -> VirtualFileDependencies.getTest()
        );
    }
}
